package com.example.myapplication.tack_08;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.myapplication.R;
import com.example.myapplication.tack_08.adapter.CommentAdapter;
import com.example.myapplication.tack_08.data.CommontData;
import com.example.myapplication.tack_08.data.DetermineData;
import com.example.myapplication.tack_08.data.XQPage;
import com.example.myapplication.utils.Common;
import com.example.myapplication.utils.OkHttpTool;
import com.example.myapplication.utils.StateData;

import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Comment;

import java.util.List;

public class XQActivity extends AppCompatActivity {
    private ImageView exit;
    private String id;
    private ImageView imags;
    private TextView content;
    private TextView ck;
    private TextView plshu;
    private RecyclerView comment;
    private List<CommontData.RowsDTO> listCommont;
    private JSONObject jsonObject;
    private EditText input;
    private TextView release;
    private String s;
    private TextView click;
    private JSONObject jsonObject2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_08_x_q);
        init();
        exit_();
        Intent intent = getIntent();
        id = intent.getStringExtra(Common.COMMON);
        getPage(id);
        getComment(id);
        //添加评论
        clickadd(id);
        //点击报名
        clickApplication(id);
        //判断是否报名
        determine(id);


    }

    private void determine(String id) {
        String url = "prod-api/api/activity/signup/check?activityId="+id;
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, DetermineData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                DetermineData determineData = (DetermineData)obj;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        deteAdapter(determineData);
                    }
                });
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void deteAdapter(DetermineData determineData) {
        if(determineData.isIsSignup()){
            click.setText("已报名");
            click.setTextColor(Color.parseColor("#000000"));
            click.setBackground(ContextCompat.getDrawable(XQActivity.this, R.drawable.grey));
        }else{
            click.setText("报名");
        }
    }

    private void clickApplication(String id) {
        click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        CharSequence text = click.getText();
//                        if("已报名".equals(text)){
//                            Toast.makeText(XQActivity.this,"该活动已报名",Toast.LENGTH_SHORT).show();
//                        }else if("报名".equals(text)){
                            try {
                                jsonObject2.put("activityId",id);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            String json = jsonObject2.toString();
                            application(id,json);
//                        }

                    }
                });
            }
        });
    }

    private void application(String id,String json) {
        String url = "prod-api/api/activity/signup";
        OkHttpTool.doNetRequest(url, OkHttpTool.POST, StateData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                StateData stateData = (StateData) obj;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(stateData.getCode() == 200){
                            Toast.makeText(XQActivity.this,stateData.getMsg(),Toast.LENGTH_SHORT).show();
                            click.setText("已报名");
                            click.setTextColor(Color.parseColor("#000000"));
                            click.setBackground(ContextCompat.getDrawable(XQActivity.this, R.drawable.grey));

                        }else{
                            Toast.makeText(XQActivity.this,stateData.getMsg(),Toast.LENGTH_SHORT).show();

                        }
                    }
                });
            }

            @Override
            public void onFailure(String msg) {

            }
        },json);
    }

    private void clickadd(String id) {
        release.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        String str = input.getText().toString();
                        try {
                            jsonObject.put("activityId",id);
                            jsonObject.put("content",str);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        String json = jsonObject.toString();
                        addComment(id,json);
                    }
                });

            }
        });
    }

    private void addComment(String id,String json) {
        String url = "prod-api/api/activity/comment";
        OkHttpTool.doNetRequest(url, OkHttpTool.POST, StateData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                StateData stateData = (StateData) obj;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        comAdatper(stateData);
                    }
                });
            }

            @Override
            public void onFailure(String msg) {

            }
        },json);


    }

    private void comAdatper(StateData stateData) {
        if(stateData.getCode() == 200){
            Toast.makeText(XQActivity.this,"发布成功",Toast.LENGTH_SHORT).show();
            plshu.setText(s);
            getComment(id);

        }else{
            Toast.makeText(XQActivity.this,stateData.getMsg(),Toast.LENGTH_SHORT).show();
        }
    }

    private void getComment(String id) {
        String url = "prod-api/api/activity/comment/list?activityId="
                +id+"&pageNum=1&pageSize=8";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, CommontData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                CommontData commontData = (CommontData)obj;
                listCommont = commontData.getRows();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        commentAdapter(commontData);
                    }
                });
            }

            @Override
            public void onFailure(String msg) {

            }
        });

    }

    private void commentAdapter(CommontData commontData) {
        CommentAdapter commentAdapter = new CommentAdapter(listCommont, XQActivity.this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(XQActivity.this);
        comment.setAdapter(commentAdapter);
        comment.setLayoutManager(linearLayoutManager);
        s = String.valueOf(commontData.getTotal());
        ck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                plshu.setText(s);
                plshu.setVisibility(View.VISIBLE);

            }
        });
    }

    private void getPage(String id) {
        String url = "prod-api/api/activity/activity/"+id;
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, XQPage.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                XQPage xqPage = (XQPage)obj;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        PageAdapter(xqPage);
                    }
                });
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void PageAdapter(XQPage xqPage) {
        Glide.with(XQActivity.this).load(OkHttpTool.URL+xqPage.getData().getImgUrl()).into(imags);
        String str = Html.fromHtml(xqPage.getData().getContent()).toString();
        content.setText(str);
    }

    private void exit_() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                XQActivity.this.finish();
            }
        });
    }

    private void init() {
        exit = findViewById(R.id.exit);
        imags = findViewById(R.id.imags);
        content = findViewById(R.id.content);
        comment = findViewById(R.id.comment);
        plshu = findViewById(R.id.plshu);
        ck = findViewById(R.id.ck);
        jsonObject = new JSONObject();
        jsonObject2 = new JSONObject();
        release = findViewById(R.id.release);
        input = findViewById(R.id.input);
        click = findViewById(R.id.click);
    }
}